package com.lu.controller;


import com.lu.mapper.NewsCommentMapper;
import com.lu.pojo.NewsComment;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "newsComment")
@RestController
@CrossOrigin
public class NewsCommentController {
    @Resource
    private NewsCommentMapper newsCommentMapper;

    @ApiOperation("获取全部评论")
    @GetMapping("/getNewsComment")
    public List<NewsComment> getNewsComment(){
        return newsCommentMapper.selectList(null);
    }
    @ApiOperation("根据新闻id获取评论")
    @GetMapping("/getNewsComment/{id}")
    public List<NewsComment> getNewsComment(@PathVariable("id")int id){
        Map<String, Object> map = new HashMap<>();
        map.put("fk_news_id",id);
        return newsCommentMapper.selectByMap(map);
    }


    @ApiOperation("添加评论")
    @GetMapping("/InsertNewsComment/{FkNewsId}/{UserName}/{FkUserId}/{Comment}")
    public int InsertNewsComment(@PathVariable("FkNewsId")int nid,@PathVariable("UserName")String name,@PathVariable("FkUserId")int uid,@PathVariable("Comment")String comment){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        NewsComment newsComment = new NewsComment();
        newsComment.setFkNewsId(nid);
        newsComment.setFkUserId(uid);
        newsComment.setUserName(name);
        newsComment.setComment(comment);
        newsComment.setCommentTime(dateFormat.format(new Date()));
        return newsCommentMapper.insert(newsComment);
    }

    @ApiOperation("根据新闻id删除评论")
    @GetMapping("/DeleteNewsComment/{id}")
    public int DeleteComment(@PathVariable("id")int id){
        Map<String, Object> map = new HashMap<>();
        map.put("fk_news_id",id);
        return newsCommentMapper.deleteByMap(map);
    }

    @ApiOperation("根据id删除评论")
    @GetMapping("/DeleteNewsComment2/{id}")
    public int DeleteComment2(@PathVariable("id")int id){
        Map<String, Object> map = new HashMap<>();
        map.put("id",id);
        return newsCommentMapper.deleteByMap(map);
    }

}
